import Layout from '@/layout'

// 要加载的视图
const navsImports = {
}

// 返回路由配置数组
export function navs(menuList) {
  const navsShow = []
  if (menuList && menuList.length) {
    menuList.forEach((item, index) => {
      navsShow.push({
        path: '/' + item.menuUrl,
        component: Layout,
        redirect: '/' + item.menuUrl + '/' + ((item.menuList == null || item.menuList.length === 0) ? 'index' : item.menuList[0].menuUrl),
        ... analysisList(item)
      })
    })
  }
  return navsShow
}

function analysisList(item, ifchildren) {
  const list = item.menuList
  const reslist = []
  if (list == null || list.length === 0) {
    if (ifchildren) {
      return undefined
    }
    reslist.push({
      path: 'index',
      component: navsImports[item.menuUrl],
      name: item.menuUrl,
      meta: { title: item.menuName, icon: item.menuIcon }
    })
    return { children: reslist }
  } else {
    list.forEach((item, index) => {
      reslist.push({
        path: item.menuUrl,
        name: item.menuUrl,
        component: navsImports[item.menuUrl],
        meta: { title: item.menuName, icon: item.menuIcon },
        ... analysisList(item, true)
      })
    })
    return {
      meta: { title: item.menuName, icon: item.menuIcon },
      name: item.menuUrl,
      children: reslist
    }
  }
}
